package com.codestudy.controller;

import com.codestudy.common.LoginUtil;
import com.codestudy.dao.LikesDao;
import com.codestudy.dto.AchieveDTO;
import com.codestudy.entity.Result;
import com.codestudy.entity.Result1;
import com.codestudy.entity.User;
import com.codestudy.service.UserService;
import com.codestudy.vo.AchieveVO;
import com.codestudy.vo.UserVO;
import com.github.pagehelper.Page;
import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户表(User)表控制层
 *
 * @author makejava
 * @since 2024-04-06 14:56:30
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/user")
@Api(tags = "用户管理")
@EnableSwaggerBootstrapUI  //开启swagger美化
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;
    @Resource
    private LikesDao likesDao;

    /**
     * 通过关键词查询用户
     */
    @PostMapping
    @ApiOperation(value = "查询用户", notes = "任何字段都可以查")
    public Result1 queryById(
            @RequestBody User user,
            @RequestParam(defaultValue = "1") int orderByBgzCount //是否按照点赞数排序 0否 1是
    ){
        log.info("通过id查询用户：{}", user);
        List<UserVO> userVOList = userService.findUser(user,orderByBgzCount);
        return Result1.success(userVOList);
    }

    /**
     * 修改用户信息
     *
     * @param user 实体
     * @return 修改结果
     */
    @PutMapping("/updateUser")
    @ApiOperation(value = "修改用户信息")
    public Result<User> edit(@RequestBody User user) {
        log.info("编辑用户：{}", user);
        userService.updateUser(user);
        return Result.ok("修改成功");
    }

    //根据文章id查询用户信息
    @ApiOperation(value = "根据文章id查询用户信息")
    @GetMapping("/getUserInfoByNoteId")
    public Result1 getUserInfoByNoteId(Integer noteId) {
        log.info("文章id:{}", noteId);
        UserVO userVO = userService.findUserByNoteId(noteId);
        return Result1.success(userVO);
    }

    //检查是否登录
    @ApiOperation(value = "检查是否登录")
    @GetMapping("/checkLogin")
    public Result1 checkLogin() {
        return Result1.success("已登录");
    }

    //添加成就
    @ApiOperation(value = "添加成就")
    @PostMapping("/addAchieve")
    public Result1 addAchieve(@RequestBody AchieveDTO achieve) {
        log.info("接收到的成就：{}", achieve);
        Result1 result1 = userService.addAchieve(achieve);
        return result1;
    }

    //查看成就
    @ApiOperation(value = "查看成就")
    @GetMapping("/findAchieve")
    public Result1 findAchieve(Integer userId) {
        List<AchieveVO> achieveDTOList = userService.findAchieve(userId);
        return Result1.success(achieveDTOList);
    }

    //根据作者id1判断是否关注了作者
    @GetMapping("/checkConcern")
    public Result1 checkConcern(Integer authorId){
        return Result1.success(likesDao.queryConcernByNoteId(authorId,LoginUtil.getCurrentId()));
    }
}

